package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.SpecificationOptionService;
import com.dongdongshop.service.SpecificationService;
import com.dongdongsop.data.AjaxResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("specification")
public class SpecificationController {

    @DubboReference
    private SpecificationService specificationService;

    @DubboReference
    private SpecificationOptionService specificationOptionService;

    //跳转规格管理首页
    @RequestMapping("toSpecification")
    public String toSpecification(){
        return "admin/specification";
    }

    //查询规格表全部
    @RequestMapping("specificationInfo")
    @ResponseBody
    public List<TbSpecification> specificationInfo(){
        List<TbSpecification> specificationList = specificationService.specificationInfo();
        return specificationList;
    }

    //跳转规格管理添加页面
    @RequestMapping("toAdd")
    public String toAdd(){
        return "specification/add";
    }

    //添加规格管理和规格子表
    @RequestMapping("add")
    @ResponseBody
    public AjaxResult add(TbSpecification specification, String options){
        try {
            List<TbSpecificationOption> list = JSONObject.parseArray(options, TbSpecificationOption.class);
            int i = specificationService.add(specification,list);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //跳转规格管理修改页面，根据ID回显
    @RequestMapping("toUpdate")
    public String toUpdate(Long id, Model model){
        TbSpecification specification = specificationService.toUpdate(id);
        List<TbSpecificationOption> specificationOptionList = specificationOptionService.getSpecId(id);
        model.addAttribute("ts",specification);
        model.addAttribute("options",specificationOptionList);
        return "specification/update";
    }

    //规格管理修改
    @RequestMapping("update")
    @ResponseBody
    public AjaxResult update(TbSpecification specification, String options){
        try {
            List<TbSpecificationOption> list = JSONObject.parseArray(options, TbSpecificationOption.class);
            int i = specificationService.update(specification,list);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //规格管理删除
    @RequestMapping("deleteId")
    @ResponseBody
    public AjaxResult deleteId(Long id){
        try {
            int i = specificationService.deleteId(id);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
